package com.liuzy.server;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

import com.liuzy.executor.WinCmder;
import com.liuzy.executor.Executor;
import com.liuzy.executor.MySqler;

/**
 * 执行Client的CMD命令
 * 
 * @author liuzy
 * @version 2015年5月1日
 */
public class Task extends Thread {
	/** 任务工作状态标志 */
	private boolean isRun = true;
	/** 客户端套节字 */
	private Socket socket;
	/** 客户端编号 */
	private int id;
	/** 客户端的输入 */
	private InputStream in;
	/** 客户端的输出 */
	private OutputStream out;
	/** 当前控制台 SERVER,CMD,SQL */
	private int console = Console.SERVER;
	/** 输入密码状态 */
	private boolean pwdStatus;
	/** 已输入密码错误次数 */
	private int pwdCount = 0;
	/** SERVER,CMD,SQL的执行者 */
	private Executor executor;

	public Task(Socket socket, int id) {
		this.socket = socket;
		this.id = id;
		try {
			this.in = socket.getInputStream();
			this.out = socket.getOutputStream();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		Server.log("客户端" + id + "已连接! " + this.toString());
		executor = new Executor(in, out);
		Scanner scanner = new Scanner(in);
		while (isRun && scanner.hasNextLine()) {
			String line = scanner.nextLine().trim();
			String[] temp = (line + " ").split(" ");
			String conmmand = temp.length > 0 ? temp[0] : line;
			String param1 = temp.length > 1 ? temp[1] : "";
			String param2 = temp.length > 2 ? temp[2] : "";
			String param3 = temp.length > 3 ? temp[3] : "";
			if (!"".equals(line))
				Server.log("客户端" + id + ":" + line);
			switch (console) {
			case Console.SERVER:
				if (pwdStatus) {
					if ("111111".equals(line)) {
						Server.shutdown();
					} else {
						++pwdCount;
						if (pwdCount > 2) {
							executor.writeMsg("错误!\r\n");
							pwdStatus = false;
							pwdCount = 0;
							executor.init();
							continue;
						}
						executor.writeMsg("再输入密码:");
					}
				}
				switch (conmmand) {
				case "":
					executor.writeMsg(">");
					break;
				case "cmd":
					executor = new WinCmder(in, out);
					console = Console.CMD;
					break;
				case "sql":
					if (!"".equals(param1) && !"".equals(param2) && !"".equals(param3))
						executor = new MySqler(in, out, param1, param2, param3);
					else
						executor = new MySqler(in, out);
					console = Console.SQL;
					break;
				case "status":
					executor.writeMsg(Server.getStatus() + "我是" + id + "号\r\n>");
					break;
				case "exit":
					executor.writeMsg("\r\n再见!\r\n");
					this.stopTask();
					break;
				case "shutdown":
					executor.writeMsg("请输入密码:");
					this.pwdStatus = true;
					break;
				case "help":
					executor.init();
					break;
				case "kill":
					int id = 0;
					try {
						id = Integer.parseInt(param1);
					} catch (Exception e) {
						executor.writeMsg("你要杀谁?\r\n>");
						continue;
					}
					if (this.id == id) {
						executor.writeMsg("不要自杀,你可以退出.\r\n>");
					} else {
						Server.stopTask(id);
						executor.writeMsg(">");
					}
					break;
				default:
					executor.writeMsg("神马?\r\n>");
					break;
				}
				break;
			case Console.CMD:
				switch (conmmand) {
				case "":
					executor.execute("");
					break;
				case "exit":
					executor.destroy();
					executor = new Executor(in, out);
					console = Console.SERVER;
					break;
				default:
					executor.execute(line);
					break;
				}
				break;
			case Console.SQL:
				switch (conmmand) {
				case "exit":
					executor.destroy();
					executor = new Executor(in, out);
					console = Console.SERVER;
					break;
				default:
					executor.execute(line);
					break;
				}
				break;
			default:
				break;
			}
		}
		scanner.close();
		executor.destroy();
		Server.removeTask(id);
		Server.log("客户端" + id + "已退出!");
	}

	public void stopTask() {
		this.isRun = false;
		try {
			socket.shutdownOutput();
			socket.shutdownInput();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("[").append(socket.getInetAddress().toString().replace("/", "")).append(":").append(socket.getPort()).append("]");
		return sb.toString();
	}

}
